<?php
namespace Home\Controller;
use Think\Controller;


class MycollectController extends Controller {
   //收藏货品
   function collectgoods(){
    $collect=M("Mycollect");
    $supplylistid=$_POST["supplylistid"];
    $userid=$_POST["userid"];
    $result=$collect->where("supplylist_id='$supplylistid' and user_id='$userid'")->select();
     if ($result) {
      	  //已经加入，
          $this->ajaxReturn(array(
                  "code"=>500,
                  "message"=>"已经收藏过了！"
                ),"JSON");    
      }
      else{
      	//，需要加入
      	    $newdata["user_id"]=$userid;
        	$newdata["supplylist_id"]=$supplylistid;
         
            $result=$collect->add($newdata);

            if ($result) {
          	# 成功加入..
          	$this->ajaxReturn(array(
                "code"=>200,
                "message"=>"成功收藏！" 
          		),"JSON");

            }else{
          		$this->ajaxReturn(array(
                "code"=>500,
                "message"=>"加入失败！"
          		),"JSON");
            }
      }

   }

    //移除货品
   function removecollectgoods(){
    $collect=M("Mycollect");
    $supplylistid=$_POST["supplylistid"];
    $userid=$_POST["userid"];
    $result=$collect->where("supplylist_id='$supplylistid' and user_id='$userid'")->select();
     if ($result) {
      	   //，需要加入
      	    $newdata["user_id"]=$userid;
        	$newdata["supplylist_id"]=$supplylistid;
            $result=$collect->where("supplylist_id='$supplylistid' and user_id='$userid'")->delete();
            if ($result) {
          	# 成功加入..
          	$this->ajaxReturn(array(
                "code"=>200,
                "message"=>"成功删除！" 
          		),"JSON");

            }else{
          		$this->ajaxReturn(array(
                "code"=>500,
                "message"=>"失败！"
          		),"JSON");
            } 
      }

   }


   //查询我收藏的货品
   function Mycollectgoods(){
    $collect=M("Mycollect");
    $userid=$_POST["userid"];
    $result=$collect
    ->field("t_address.address_name,supplylist_price,supplylist_goodsname,supplylist_time,
      supplylist_desc,supplylist_image,supplylist_minnumber,user_nick,t_supplylist.supplylist_id")
    ->join("t_supplylist on t_mycollect.supplylist_id=t_supplylist.supplylist_id")
    ->join("t_user on t_mycollect.user_id = t_user.user_id")
    ->join("t_address on t_supplylist.address_id=t_address.address_id")
    ->where("t_user.user_id='$userid'")->select();
     if ($result) {
      	# 成功加入..
      	$this->ajaxReturn(array(
            "code"=>200,
            "message"=>"成功！" ,
            "result"=>$result
      		),"JSON");

        }else{
      		$this->ajaxReturn(array(
            "code"=>500,
            "message"=>"失败！"
      		),"JSON");
        } 
   }





}